package world.geometry;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;

public class Room extends TransformGroup {

    public Room(){
        this(new Vector3d(10.0f, 2.0f, 10.0f));
    }

    public Room(Vector3d size){

        super();
        
    	this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    	
        Shape3D walls = new Shape3D();
    	walls.setGeometry(new Walls());
    	walls.setAppearance(createWallsAppearance());
    	this.addChild(walls);

        Shape3D floor_ceiling = new Shape3D();
    	floor_ceiling.setGeometry(new FloorCeiling());
    	floor_ceiling.setAppearance(floorCeilingAppearance());
    	this.addChild(floor_ceiling);

    	Transform3D transform = new Transform3D();
    	transform.setScale(size);
    	this.setTransform(transform);

    }

    private Appearance createWallsAppearance() {

        Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
        Color3f emmisiveColor = new Color3f(0f, 0f, 0f);
        Color3f diffuseColor = new Color3f(0.0f, 0.2f, 0.5f);
	    Color3f specularColor = new Color3f(0.0f, 0.0f, 0.0f);
        float shininess = 1.0f;

        Appearance app = new Appearance();

        Material mat = new Material(ambientColor, emmisiveColor, diffuseColor, specularColor, shininess);
    	mat.setLightingEnable(true);
        app.setMaterial(mat);

    	ColoringAttributes ca = new ColoringAttributes ();
        ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
    	app.setColoringAttributes(ca);
        
        PolygonAttributes pa = new PolygonAttributes ();
    	pa.setCullFace(PolygonAttributes.CULL_BACK);
    	app.setPolygonAttributes(pa);

    	//TextureLoader loader = new TextureLoader("crazy-green.jpg", WorldMain.getInstance());
        //Texture brick = loader.getTexture();

    	TextureAttributes ta = new TextureAttributes();
    	ta.setTextureMode(TextureAttributes.BLEND);
    	app.setTextureAttributes(ta);
        //app.setTexture(brick);

        return app;
    }

    private Appearance floorCeilingAppearance () {

    	Appearance app = new Appearance();
    	Color3f ambientColor = new Color3f (0.3f, 0.3f, 0.3f);
        Color3f emmisiveColor = new Color3f (0f, 0f, 0f);
        Color3f diffuseColor = new Color3f (0.0f, 0.0f, 1.0f);
    	Color3f specularColor = new Color3f (0.0f, 0.0f, 0.0f);
        float shininess = 1.0f;
    	Material mat = new Material(ambientColor, emmisiveColor, diffuseColor, specularColor, shininess);
    	mat.setLightingEnable(true);
    	app.setMaterial(mat);
    	ColoringAttributes ca = new ColoringAttributes ();
        ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
    	app.setColoringAttributes(ca);
        PolygonAttributes pa = new PolygonAttributes ();
    	pa.setCullFace(PolygonAttributes.CULL_NONE);
    	app.setPolygonAttributes(pa);
        return app;
    }
}
